package com.example.demo.controller;

import com.example.demo.mapper.HelloMapper;
import com.example.demo.model.HelloModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author LiYiGuang
 * @date 2019/4/29 15:30
 */
@RestController
@RequestMapping("hello")
public class HelloController {
    @Autowired
    private HelloMapper helloMapper;
    @RequestMapping("index")
    public String index() {
        return "Hello World";
    }
    @RequestMapping("list")
    public List<HelloModel> list() {
        return helloMapper.selectAll();
    }
    @RequestMapping("get/{id}")
    public HelloModel get(@PathVariable int id) {
        return helloMapper.select(id);
    }
    @RequestMapping("update/{id}/{title}/{text}")
    public HelloModel update(@PathVariable int id,@PathVariable String title,@PathVariable String text) {
        HelloModel helloModel = new HelloModel();
        helloModel.setId(id);
        helloModel.setTitle(title);
        helloModel.setText(text);
        int i= helloMapper.updateValue(helloModel);
        return helloMapper.select(id);
    }

    @RequestMapping("delete/{id}")
    public List<HelloModel> delete(@PathVariable int id) {
        int i = helloMapper.delete(id);
        return helloMapper.selectAll();
    }
    @RequestMapping("insert/{title}/{text}")
    public List<HelloModel> insert(@PathVariable String title,@PathVariable String text) {
        HelloModel helloModel = new HelloModel();
        helloModel.setTitle(title);
        helloModel.setText(text);
        int i= helloMapper.insert(helloModel);
        return helloMapper.selectAll();
    }
}
